package client;
import java.util.Scanner;

public class Client {

	public static void main(String args[]) {
		Client c = new Client();
		c.start();
	}

	private Connection connection = new Connection();
	private String userName;
	private boolean editPermission;

	private void start() {
		if (connectToserver()) {
			if (login()) {
				run();
			}
		}
	}

	/**
	 * Startar en anslutning till servern och verifierar äkthet.
	 */
	private boolean connectToserver() {
		System.out.println("Connecting to server...");
		boolean b = connection.connect();
		if (b)
			System.out.println("Connected");
		else
			System.out.println("Failed to connect");
		return b;
	}

	/**
	 * Kollar användardata mot servern.
	 */
	private boolean login() {
		System.out.println("Log in");
		userName = readUser();
		boolean b = connection.login(userName);
		if (b) {
			System.out.println("Logged in");
			editPermission = connection.getPermission(userName);
		} else {
			System.out.println("Log in failed!");
		}
		return b;
	}

	private String readUser() {
		Scanner scan = new Scanner(System.in);
		System.out.print("\nusername: ");
		return scan.next();
	}

	/**
	 * Visar meny
	 */
	private void run() {
		int choice = -1;
		while (choice != 0) {
			printMenu();
			choice = handleChoice();
		}
		System.out.println("Exiting");
		System.out.println("Exited - Goodbye ");
	}

	private int handleChoice() {
		int choice = -1;
		Scanner scan = new Scanner(System.in);
		String temp = scan.next();
		try {
			choice = Integer.parseInt(temp);
		} catch (NumberFormatException e) {
			System.out.println("Not valid input: " + temp);
		}

		switch (choice) {
		case 1:
			listJournals();
			break;
		case 2:
			showJournal();
			break;
		case 3:
			editJournal();
			break;
		case 0:
			break;
		}
		return choice;
	}

	private void showJournal() {
		String journal = getJournal();
		EditWindow w = new EditWindow();
		w.showText(journal);
	}

	/**
	 * Print menu
	 */
	private void printMenu() {
		System.out.println("\nAvailable options: ");
		System.out.println("[1] Show available journals");
		System.out.println("[2] View a journal");
		if (editPermission)
			System.out.println("[3] Edit a journal");
		System.out.println("[0] Exit");
		System.out.print("\n\n Choice: ");
	}

	/**
	 * Edit a journal
	 */
	private void editJournal() {
		String journal = getJournal();
		EditWindow w = new EditWindow();
		journal = w.editText(journal);
		if (journal != "") {
			System.out.println("Contacting server... [Edit]");
			if (connection.applyEditJournal(journal, userName)) {
				System.out.println("Success, journal has been edited!");
			} else {
				System.out.println("Failed [Edit].");
			}
		}
	}

	private String getJournal() {
		Scanner scan = new Scanner(System.in);
		System.out.print("\nEnter journal name: ");
		String journalName = scan.next();
		System.out.println("Contacting server... [View journal]");
		String journal = connection.applyForJournal(journalName, userName);
		System.out.println("Loading journal...");
		return journal;
	}

	private void listJournals() {
		System.out.println("Contacting server... [View list]");
		String list = connection.applyForList(userName);
		if (list == "") {
			System.out.println("Failed [View List]");
		} else {
			System.out.println("Success [View List]");
			for (String s : parse(list)) {
				System.out.println(" - " + s);
			}
		}

	}

	private String[] parse(String list) {
		return list.split(":");
	}
}
